﻿using System;
using TeacherHelp.DAL;
using TeacherHelp.Util;

namespace TeacherHelp.BLL
{
    public class LoginManager
    {
        private readonly UserService _userService;
        public LoginManager()
        {
            _userService = new DAL.UserService();
        }
        public (string, int, int) Login(Model.User user)
        {
            string erroInfo = "用户名或密码错误！";
            user.LoginPwd = MD5Helper.encrypt(user.LoginPwd);
            string sql = _userService.GetUserInfoByLoginNameAndLoginPwdSQL(user.LoginName, user.LoginPwd);

            Model.User model = null;
            try
            {
                model = DBHelper.GetOneModel<Model.User>(sql);
            }
            catch (Exception ex)
            {
                erroInfo = ex.Message;
                Log.Error("登录时出现异常！", ex);
            }

            

            if (model != null)
            {
                Log.Info($"userId={model.Id},进行了登录操作！");
                return (null, Convert.ToInt32(model.RoleId), model.Id);
            }
            return (erroInfo, default(int), default(int));
        }
    }
}
